<template>
    <a-modal
        v-model:visible="showModal"
        :title="title"
        :width="520"
        :destroyOnClose="true"
        :maskClosable="false"
        :confirmLoading="loading"
        @ok="onSubmit"
        @cancel="handleCancle"
    >
        <a-form class="dept-model" layout="horizontal" :model="formState" :rules="rules" ref="formRef">
            <a-form-item class="full-width" :label="t('yl.deviceCode')" name="deviceCode">
                <a-input v-model:value="formState.deviceCode" :placeholder="t('yl.deviceCode')" />
            </a-form-item>
            <a-form-item class="full-width" :label="t('yl.nfcCode')" name="nfcCode">
                <a-input v-model:value="formState.nfcCode" :placeholder="t('yl.nfcCode')" />
            </a-form-item>
            <a-form-item class="full-width" :label="t('yl.equipmentModel')" name="specsCode">
                <a-select v-model:value="formState.specsCode" class="full-width" :placeholder="t('yl.equipmentModel')">
                    <a-select-option v-for="(item, index) in models" :value="item.specsCode" :key="index">
                        {{ item.tagMeasureName + '-' + item.tagColourName }}
                    </a-select-option>
                </a-select>
            </a-form-item>
        </a-form>
    </a-modal>
</template>
<script lang="ts">
import { computed, defineComponent, reactive, ref, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import _ from 'lodash';
import ylTreeselectVue from '@/components/ylTreeselect.vue';

interface DeptState {
    deviceCode: string;
    specsCode: string | number;
    id: string | number;
    nfcCode: string | number;
}

// 表单校验规则
const initRules = (t: fn) => {
    return {
        deviceCode: [{ required: true, message: t('yl.inputNameStand'), trigger: 'blur' }],
        specsCode: [{ required: true, message: t('yl.deptSortStand'), trigger: 'blur' }],
    };
};

export default defineComponent({
    name: 'roleModel',
    components: {
        ylTreeselectVue,
    },
    props: {
        visible: {
            type: Boolean,
            default: () => false,
        },
        title: {
            type: String,
        },
        selectEditModel: {
            type: Array,
            default: () => [],
        },
        loading: {
            type: Boolean,
            default: () => false,
        },
        dept: {
            type: Object,
        },
    },
    emits: ['submit', 'cancle'],
    setup(props, { emit }) {
        const { t } = useI18n();
        // const a = reactive({
        //     showModal: computed(() => props.visible),
        //     models: computed(() => props.selectModels as NormalObj[]),
        // })
        const showModal = computed(() => props.visible);
        let models = computed(() => props.selectEditModel as NormalObj[]);
        const formState: DeptState = reactive({
            deviceCode: '',
            specsCode: '',
            id: '',
            nfcCode: '',
        });

        // 编辑时赋值
        watch(
            () => props.dept,
            (dept) => {
                if (_.isEmpty(dept)) return;
                const { deviceCode, specsCode, id, nfcCode } = dept as DeptState;
                formState.deviceCode = deviceCode;
                formState.specsCode = specsCode;
                formState.id = id;
                formState.nfcCode = nfcCode;
            }
        );

        // 表单校验
        const rules = initRules(t);
        const formRef = ref();
        // 提交
        const onSubmit = () => {
            formRef.value.validate().then(() => {
                emit('submit', formState);
            });
        };

        // 取消
        const handleCancle = () => {
            return emit('cancle');
        };

        return {
            formRef,
            labelCol: { span: 8 },
            wrapperCol: { span: 16 },
            showModal,
            models,
            formState,
            t,
            rules,
            onSubmit,
            handleCancle,
        };
    },
});
</script>
<style lang="scss" scoped>
.dept-model {
    @include flexWrap;
    width: 100%;
    .ant-form-item {
        @include flex;
        width: 50%;
        margin-right: 0;
        padding-right: $width10;
        margin-bottom: $width8;
        :deep .ant-form-item-label {
            flex-basis: $formLabel;
        }
        :deep .ant-form-item-control-wrapper {
            flex: 1;
        }
        :deep .ant-form-item-children,
        :deep .ant-input-number {
            width: 100%;
        }
        &.full-width {
            width: 100%;
        }
    }
}
</style>
